package com.zmn.oms.admin.controller.conf.distribute;

import com.zmn.base.common.data.common.dio.company.CompanyDIO;
import com.zmn.base.common.data.common.dio.company.CompanyTypeDIO;
import com.zmn.base.common.data.dubbo.interfaces.area.AreaListRemoteService;
import com.zmn.base.common.data.dubbo.interfaces.company.CompanyListRemoteService;
import com.zmn.base.plat.engine.common.constant.CompanyConsts;
import com.zmn.base.product.dubbo.interfaces.product.foreign.ProductGroupForeignListRemoteService;
import com.zmn.common.dto.VtDTO;
import com.zmn.common.dto2.OptionData;
import com.zmn.common.dto2.ResponseDTO;
import com.zmn.common.utils.collection.CollectionUtil;
import com.zmn.manager.redis.RedisManager;
import com.zmn.mcc.cas.model.LoginStaff;
import com.zmn.mcc.cas.web.StaffAdminService;
import com.zmn.mcc.common.constant.DpermitConsts;
import com.zmn.mcc.common.dto.permit.SpermitDTO;
import com.zmn.mcc.dubbo.dto.DubboConsts;
import com.zmn.mcc.dubbo.interfaces.staff.StaffPermitRemoteService;
import com.zmn.oms.business.interfaces.conf.distribute.DistributeWeightPlatBService;
import com.zmn.oms.business.interfaces.conf.distribute.NonGridRobOrderConfigBService;
import com.zmn.oms.business.interfaces.conf.distribute.RobOrderConfigBService;
import com.zmn.oms.services.interfaces.conf.distribute.DistributeWeightCompanyService;
import com.zmn.oms.services.interfaces.conf.distribute.DistributeWeightPlatService;
import com.zmn.oms.services.interfaces.conf.distribute.DistributeWeightRelationService;
import com.zmn.sp.dubbo.interfaces.available.AvailableAreaListRemoteService;
import com.zmn.sp.dubbo.interfaces.manage.SpManageListRemoteService;
import org.apache.commons.collections.CollectionUtils;
import org.apache.dubbo.config.annotation.DubboReference;
import org.apache.dubbo.config.annotation.Reference;

import javax.annotation.Resource;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;
import java.util.Objects;
import java.util.stream.Collectors;

/**
 * 类描述：派单参数配置基础控制器
 *
 * @author mujunlin
 * @version v1.0
 * @since 2019/10/17 14:15
 */
public class DistributeWeightBaseController {

    @DubboReference(version = com.zmn.sp.dubbo.utils.DubboConsts.INTERFACE_VERSION,check = false)
    protected AvailableAreaListRemoteService availableAreaListRemoteService;

    @DubboReference(version = com.zmn.base.plat.engine.common.constant.DubboConsts.INTERFACE_VERSION, check = false, timeout = 200000)
    AreaListRemoteService areaListRemoteService;

    @DubboReference(version = com.zmn.base.plat.engine.common.constant.DubboConsts.INTERFACE_VERSION, check = false)
    CompanyListRemoteService companyListRemoteService;
    @DubboReference(version = com.zmn.sp.dubbo.utils.DubboConsts.INTERFACE_VERSION, check = false)
    private SpManageListRemoteService spManageListRemoteService;

    @DubboReference(version = com.zmn.base.plat.engine.common.constant.DubboConsts.INTERFACE_VERSION , check = false,timeout = 200000)
    ProductGroupForeignListRemoteService productGroupForeignListRemoteService;

    @Resource
    protected DistributeWeightPlatBService distributeWeightPlatBService;
    @Resource
    protected DistributeWeightCompanyService distributeWeightCompanyService;

    @Resource
    protected DistributeWeightPlatService distributeWeightPlatService;

    @Resource
    protected DistributeWeightRelationService distributeWeightRelationService;

    @Reference(version = DubboConsts.INTERFACE_VERSION, check = false)
    StaffPermitRemoteService staffPermitRemoteService;

    @Resource
    protected RobOrderConfigBService robOrderConfigBService;

    @Resource
    protected NonGridRobOrderConfigBService nonGridRobOrderConfigBService;

    @Resource
    protected RedisManager redisManager;

    /**
     * 根据员工ID获取员工拥有的平台权限
     *
     * @param staff
     *            员工
     * @return 拥有权限的平台ID集合
     */
    protected List<Integer> listPermitedPlatByStaff(LoginStaff staff) {

        SpermitDTO spermitDTO = staffPermitRemoteService.listDpermitByLoginStaff(staff, DpermitConsts.TYPE_ORGANIZE_PLAT, null);

        // 无数据权限
        if (Objects.equals(spermitDTO.getRange(), SpermitDTO.RANGE_EMPTY)) {
            return Collections.emptyList();
        }

        // 全部数据权限
        if (Objects.equals(spermitDTO.getRange(), SpermitDTO.RANGE_ALL)) {
            return null;
        }

        // 正常权限 但是权限数据集合为空 视为 无权限则直接返回
        if (Objects.equals(spermitDTO.getRange(), SpermitDTO.RANGE_NORMAL) && CollectionUtils.isEmpty(spermitDTO.getItems())) {
            return Collections.emptyList();
        }

        // 正常权限
        return spermitDTO.getItems();

    }

    /**
     * 加载所有城市
     *
     * @return 所有城市信息
     */
    List<VtDTO> loadAllCityData() {
        ResponseDTO<OptionData<Integer>> listResponseDTO = areaListRemoteService.listOptionAllCity();
        if (Objects.isNull(listResponseDTO.getData()) || CollectionUtil.isNullOrEmpty(listResponseDTO.getData().getOptions())) {
            return Collections.emptyList();
        }
        return listResponseDTO.getData().getOptions()
                .stream()
                .map(e -> new VtDTO(e.getValue(), e.getLabel(), e.getDisabled()))
                .collect(Collectors.toList());
    }

    /**
     * 根据平台查询平台下的公司
     *
     * @param plat
     *            平台ID
     * @return 平台下的公司集合
     */
    List<VtDTO> listCompanyByPlat(Integer plat) {
        List<CompanyTypeDIO> typeQueryList = Arrays.asList(
                new CompanyTypeDIO(CompanyConsts.COMPANY_TYPE_SERVICE, CompanyConsts.COMPANY_TYPE_SERVICE_OCO),
                new CompanyTypeDIO(CompanyConsts.COMPANY_TYPE_SERVICE, CompanyConsts.COMPANY_TYPE_SERVICE_NOT_OCO));

        CompanyDIO companyDIO = new CompanyDIO();
        companyDIO.setPlat(plat);
        companyDIO.setCompanyTypeQueryList(typeQueryList);
        companyDIO.setPageSize(1000);

        ResponseDTO<OptionData<Integer>> listResponseDTO = companyListRemoteService.listPageOptionByCompanyDIO(companyDIO);
        if (Objects.isNull(listResponseDTO.getData()) || CollectionUtil.isNullOrEmpty(listResponseDTO.getData().getOptions())) {
            return Collections.emptyList();
        }
        return listResponseDTO.getData().getOptions()
                .stream()
                .map(e -> new VtDTO(e.getValue(), e.getLabel(), e.getDisabled()))
                .collect(Collectors.toList());
    }

    /**
     * 获取登录用户
     *
     * @return
     */
    public LoginStaff getMcStaff() {
        return StaffAdminService.currentStaff();
    }

    /**
     * 获取登录用户名
     *
     * @param
     * @return
     */
    public String getMcStaffRealName() {
        LoginStaff staff = StaffAdminService.currentStaff();
        if(Objects.nonNull(staff)){
            return staff.getRealName();
        }else {
            return "";
        }
    }

}
